package utils;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class CapturarTeclado extends KeyAdapter {
	
		private boolean waitingForKeyPress = false;
		private boolean leftPressed = false;
		private boolean rightPressed = false;
		private boolean upPressed = false;
		private boolean downPressed = false;
		private boolean firePressed = false;
		private boolean empezar = false;
		private int leftKey;
		private int rightKey;
		private int downKey;
		private int upKey;
		private int fireKey ;
		private Character teclaPulsada;
	
		
		
		public CapturarTeclado(int leftKey,int rightKey,int downKey, int upKey, int fireKey)
		{
			this.leftKey = leftKey;
			this.rightKey = rightKey;
			this.upKey = upKey;
			this.downKey = downKey;
			this.fireKey = fireKey;
		
		}
		
		/**
		 * Notification from AWT that a key has been pressed. Note that
		 * a key being pressed is equal to being pushed down but *NOT*
		 * released. Thats where keyTyped() comes in.
		 *
		 * @param e The details of the key that was pressed 
		 */
		public void keyPressed(KeyEvent e) {
			
			if (waitingForKeyPress) {
				return;
			}
			
			if (e.getKeyCode() == leftKey) {
				setLeftPressed(true);
			}
			if (e.getKeyCode() == rightKey) {
				setRightPressed(true);
			}
			if (e.getKeyCode() == downKey) {
				setDownPressed(true);
			}
			if (e.getKeyCode() == upKey) {
				setUpPressed(true);
			}
			if (e.getKeyCode() == fireKey) {
				setFirePressed(true);
			}
		} 
		
		/**
		 * Notification from AWT that a key has been released.
		 *
		 * @param e The details of the key that was released 
		 */
		public void keyReleased(KeyEvent e) {
			// if we're waiting for an "any key" typed then we don't 
			// want to do anything with just a "released"
			if (waitingForKeyPress) {
				return;
			}
			
			if (e.getKeyCode() == leftKey) {
				setLeftPressed(false);
			}
			if (e.getKeyCode() == rightKey) {
				setRightPressed(false);
			}
			if (e.getKeyCode() == downKey) {
				setDownPressed(false);
			}
			if (e.getKeyCode() == upKey) {
				setUpPressed(false);
			}
			if (e.getKeyCode() == fireKey) {
				setFirePressed(false);
			}
		}

		/**
		 * Notification from AWT that a key has been typed. Note that
		 * typing a key means to both press and then release it.
		 *
		 * @param e The details of the key that was typed. 
		 */
		public void keyTyped(KeyEvent e) {
			if (waitingForKeyPress) {
					waitingForKeyPress = false;
					setTeclaPulsada(e.getKeyChar());
			}
			if (e.getKeyChar() == 27) {
				System.exit(0);
			}
		}

		public boolean isLeftPressed() {
			return leftPressed;
		}

		public void setLeftPressed(boolean leftPressed) {
			this.leftPressed = leftPressed;
		}

		public boolean isRightPressed() {
			return rightPressed;
		}

		public void setRightPressed(boolean rightPressed) {
			this.rightPressed = rightPressed;
		}

		public boolean isWaitingForKeyPress() {
			return waitingForKeyPress;
		}

		public void setWaitingForKeyPress(boolean waitingForKeyPress) {
			this.waitingForKeyPress = waitingForKeyPress;
		}

		public boolean isUpPressed() {
			return upPressed;
		}

		public void setUpPressed(boolean upPressed) {
			this.upPressed = upPressed;
		}

		public boolean isDownPressed() {
			return downPressed;
		}

		public void setDownPressed(boolean downPressed) {
			this.downPressed = downPressed;
		}

		public boolean isFirePressed() {
			return firePressed;
		}

		public void setFirePressed(boolean firePressed) {
			this.firePressed = firePressed;
		}

		public boolean isEmpezar() {
			return empezar;
		}

		public void setEmpezar(boolean empezar) {
			this.empezar = empezar;
		}

		public Character getTeclaPulsada() {
			return teclaPulsada;
		}

		public void setTeclaPulsada(Character teclaPulsada) {
			this.teclaPulsada = teclaPulsada;
		}
	}